//
//  ZCMechanismVC.m
//  zctx
//
//  Created by apple on 15/12/10.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMechanismVC.h"
#import "ZCMechanismProjectCell.h"
@interface ZCMechanismVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSDictionary *dataDict;
@end

@implementation ZCMechanismVC


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCMechanismVC"];
    [MTA trackPageViewBegin:@"ZCMechanismVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCMechanismVC"];
    [MTA trackPageViewEnd:@"ZCMechanismVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"检测机构";
    self.dataDict = [NSDictionary dictionary];
    [self initnavegation];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self initrequst];
    });
    
    //添加tableview
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0,0));
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCMechanismProjectCell" bundle:nil] forCellReuseIdentifier:@"mechcell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)initrequst{
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    [NetworkManager requestFoundingGetOneinstitutionWithId:self.pid Mid:UserModel.mid password:UserModel.password success:^(NSDictionary *dictResponse) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        self.dataDict = [dictResponse copy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
    }];
}


#pragma mark - UITableView DataSource & Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataDict.count>0) {
        return 1;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentity = @"mechcell";
    ZCMechanismProjectCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[ZCMechanismProjectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString *image = [NSString stringWithFormat:@"%@",[self.dataDict objectForKey:@"logo"]];
    NSString *title = [NSString stringWithFormat:@"%@",[self.dataDict objectForKey:@"name"]];
    NSString *content = [NSString stringWithFormat:@"%@",[self.dataDict objectForKey:@"content"]];

    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[NetworkManager strURLBasic],image]]];
    [cell.titleLable setText:title];
    [cell.contentLable setText:content];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *reuseIdentifier = @"mechcell";
    ZCMechanismProjectCell *cell = [self.tableView dequeueReusableCellWithIdentifier:  reuseIdentifier];
    
    NSString *image = [NSString stringWithFormat:@"%@",[self.dataDict objectForKey:@"logo"]];
    NSString *title = [NSString stringWithFormat:@"%@",[self.dataDict objectForKey:@"name"]];
    NSString *content = [NSString stringWithFormat:@"%@",[self.dataDict objectForKey:@"content"]];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[NetworkManager strURLBasic],image]]];
    [cell.titleLable setText:title];
    [cell.contentLable setText:content];
    
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    // 得到cell的contentView需要的真实高度
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    NSLog(@"heithy = %f",height);
    // 要为cell的分割线加上额外的1pt高度。因为分隔线是被加在cell底边和contentView底边之间的。
    height += 1.0f;
    
    return height;
}

#pragma mark - instance

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
